//
//  ZDQRCodeController.m
//  ZDIPhone
//
//  Created by mac on 2019/11/13.
//  Copyright © 2019 北京定辉文化传媒有限公司. All rights reserved.
//

#import "YSQRCodeController.h"
#import "YSQRCodeView.h"
#import "YSQRCodeManager.h"
#import "YSQRCodeConfiguration.h"
#import "YSCheck.h"

@interface YSQRCodeController ()

/// nav
@property (nonatomic, strong) UIButton *backView;
/// 扫描框
@property (nonatomic, strong) YSQRCodeView *scanView;
/// manager
@property (nonatomic, strong) YSQRCodeManager *manager;
/// 相册
@property (nonatomic, strong) UIButton *albumButton;
/// 相册
@property (nonatomic, strong) UIButton *brightnessButton;

@end

@implementation YSQRCodeController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    /// 二维码开启方法
    if (self.manager) [self.manager startRunning];
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    [self.scanView startScan];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [self.scanView removeScan];
    [self.manager stopRunning];
}

- (void)dealloc {
    
    [self removeScanningView];
}

- (void)removeScanningView {
    
    [self.scanView removeScan];
    [self.scanView removeFromSuperview];
    self.scanView = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.manager = [YSQRCodeManager manager];
    __weak typeof(self) weakSelf = self;
    [self.manager scanQRCodeWithController:self completion:^(YSQRCodeManager *manager, NSString *result) {
        if (result) [manager stopRunning];
        [weakSelf dealHandle:result];
    } scanBrightness:^(YSQRCodeManager *manager, CGFloat brightness) {
        /// 光线强弱处理
        if (self.brightnessButton.selected == NO) {
            self.brightnessButton.hidden = !(brightness < -1);
        } else {
            self.brightnessButton.hidden = NO;
        }
    }];
    if (![YSCheck isBlankString:self.hintText]) {
        self.scanView.hintText = self.hintText;
    }
    [self.view addSubview:self.scanView];
    [self.scanView addSubview:self.backView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([YSFrameAdapter kFitWidth_x:9]);
        make.top.mas_equalTo([YSFrameAdapter kStatusBarHeight]+11);
    }];
    [self.scanView addSubview:self.albumButton];
    [self.albumButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-([YSFrameAdapter kIPhoneXBottomSafeHeight]+[YSFrameAdapter kFitWidth_x:36]));
    }];
    
    [self.scanView addSubview:self.brightnessButton];
    [self.brightnessButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-[YSFrameAdapter kFitWidth_x:9]);
        make.top.mas_equalTo([YSFrameAdapter kStatusBarHeight]+11);
    }];
}

- (void)popAction {
    
    if (self.presentingViewController != nil) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/// 选择相册
- (void)chooseAlbumAction {
    
    __weak typeof(self) weakSelf = self;
    [self.manager establishAuthorizationQRCodeObtainAlbumWithController:self];
    if (self.manager.isPHAuthorization == YES) {
        [self.scanView removeScan];
    }
    [self.manager setBlockWithQRCodeObtainAlbumDidCancelImagePickerController:^(YSQRCodeManager *obtain) {
        [weakSelf.scanView startScan];
    }];
    [self.manager setBlockWithQRCodeObtainAlbumResult:^(YSQRCodeManager *obtain, NSString *result) {
        [weakSelf dealHandle:result];
    }];
}

/// 闪光灯
- (void)brightnessAction:(UIButton *)button {
    
    if (button.selected == NO) {
        [self.manager openFlashlight];
    } else {
        [self.manager closeFlashlight];
    }
    button.selected = !button.selected;
}

- (void)dealHandle:(NSString *)result {
    
    if (self.codeCompletion) self.codeCompletion(result, self);
}

- (void)resetQRCode {
    
    [self.scanView removeScan];
    [self.scanView startScan];
    [self.manager startRunning];
}

- (YSQRCodeView *)scanView {
    
    if (!_scanView) {
        _scanView = [[YSQRCodeView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    }
    return _scanView;
}

- (UIButton *)backView {
    
    if (!_backView) {
        _backView = [[UIButton alloc] init];
        [_backView setImage:[UIImage imageNamed:@"qrcode_back_white"] forState:UIControlStateNormal];
        _backView.ys_touchAreaSize = CGSizeMake(50, 50);
        _backView.adjustsImageWhenHighlighted = NO;
        [_backView addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backView;
}

- (UIButton *)albumButton {
    
    if (!_albumButton) {
        _albumButton = [[UIButton alloc] init];
        [_albumButton setImage:[UIImage imageNamed:@"qrcode_album"] forState:UIControlStateNormal];
        [_albumButton addTarget:self action:@selector(chooseAlbumAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _albumButton;
}

- (UIButton *)brightnessButton {
    
    if (!_brightnessButton) {
        _brightnessButton = [[UIButton alloc] init];
        _brightnessButton.hidden = YES;
        [_brightnessButton setImage:[UIImage imageNamed:@"qrcode_white_brightness"] forState:UIControlStateNormal];
        [_brightnessButton addTarget:self action:@selector(brightnessAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _brightnessButton;
}

@end
